package vno.fux.character
{
	import com.mosesSupposes.bitmap.InteractivePNG;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import vno.fux.CompassDirection;
	import vno.fux.GlobalEventScheduler;
	import vno.fux.res.pool.AnimeMatrixAttacher;
	import vno.fux.res.pool.BitmapAttacher;
	import vno.fux.res.pool.PNGAttacher;
	import vno.fux.res.pool.PlayerResource;
	import vno.fux.res.pool.ResourceAttacher;
	import vno.fux.res.pool.ResourcePool;
	import vno.fux.utils.BaseAnimation;
	import vno.fux.utils.BitmapUtils;
	
	public class PlayerView extends BaseCharacterView
	{
		private var _actionArry:Array = new Array;				//八方向行走的八个动画对象数组
		private var _currentAction:BaseAnimation;				//人物当前行走的动画对象

		public function PlayerView(model_:BaseCharacterModel, controller_:BaseCharacterController)
		{
			super(model_, controller_);
			this.mouseEnabled = false;
			var animeMatrix:AnimeMatrixAttacher = new AnimeMatrixAttacher;
			animeMatrix.addEventListener(ResourceAttacher.RECIEVE, doAnime);
			(ResourcePool.getInstance().chaRes.getRes(_model.resId) as PlayerResource).walkAnimeMatrix.attach(animeMatrix);
		}
		private function doAnime(event:Event):void
		{
			event.target.removeEventListener(BitmapAttacher.RECIEVE, doAnime);
			var bitmapDataMatrix:Vector.<Vector.<BitmapData>> = (event.target as AnimeMatrixAttacher).bitmapDataMatrix;
			for (var i:int=0; i<bitmapDataMatrix.length; i++)
			{
				var bitmapDataVector:Vector.<BitmapData> = bitmapDataMatrix[i];
				var bitmapArray:Array = [];
				for(var j:int=0; j<bitmapDataVector.length; j++)
				{
					bitmapArray[j] = new Bitmap(bitmapDataVector[j]);
				}
				var anime:BaseAnimation =new BaseAnimation(bitmapArray);
				anime.loop = true;
				anime.frameRate = 3;
				anime.mouseEnabled = false;
				_actionArry.push(anime);
			}
			_currentAction = _actionArry[6];
			this.content = _currentAction;
			this.direction = (_model as PlayerModel).currentDirection;
			this.setRegPoint([64,110]);
		}
		
		public function get direction():String
		{
			
			return (this._model as PlayerModel).currentDirection;
		}
		
		public function set direction(direct:String):void
		{
			if((this._model as PlayerModel).currentDirection == direct) return;
			if(!_currentAction) return;
			(this._model as PlayerModel).currentDirection = direct;
			_currentAction.stop();
			
			switch(direct)
			{
				case CompassDirection.NW:
					_currentAction = _actionArry[1];
					break;
				case CompassDirection.N:
					_currentAction = _actionArry[2];
					break;
				case CompassDirection.NE:
					_currentAction = _actionArry[3];
					break;
				case CompassDirection.E:
					_currentAction = _actionArry[4];
					break;
				case CompassDirection.SE:
					_currentAction = _actionArry[5];
					break;
				case CompassDirection.S:
					_currentAction = _actionArry[6];
					break;
				case CompassDirection.SW:
					_currentAction = _actionArry[7];
					break;
				case CompassDirection.W:
					_currentAction = _actionArry[0];
					break;
				default:
					break;
			}
			_currentAction.play();
			this.content = _currentAction;
		}
		
		public function move():void
		{
			if(!_currentAction) return;
			_currentAction.play();
			GlobalEventScheduler.getInstance().addEnterFrameListener(frameUpdate);
		}
		
		public function frameUpdate(evt:Event):void
		{
			_currentAction.update();
		}
		override public function update(evt:Event):void
		{
			
		}
		
		public function stop():void
		{
			if(!_currentAction) return;
			_currentAction.stop();
			GlobalEventScheduler.getInstance().removeEnterFrameListener(frameUpdate);
		}
	}
}